package com.yx.customized;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.yx.customized.avi.LoadingIconView;

/**
 * Created: _Yx
 * Time: 2018/8/16.18:01
 * Instructions:交替线；与加载框、等待框一个意思用法
 */
public class DialogAlternatinglineUtil {

    private static Dialog dialog;

    public static Context mContext;
    public static LoadingIconView avi;

    public static void showLoading(Context context) {
        // ==============原版
        // View v = LayoutInflater.from(context).inflate(R.layout.load_ing,
        // null);
        // dialog = new AlertDialog.Builder(context).show();
        // // 触摸dialog外边时dialog是否消失
        // dialog.setCanceledOnTouchOutside(false);
        // Window w = dialog.getWindow();
        // w.setContentView(v);
        // ==============end

        // ====现版
        mContext = context;
        View v = LayoutInflater.from(context).inflate(
                R.layout.dialog_alternating_line, null);
        avi = (LoadingIconView) v.findViewById(R.id.avi);

        dialog = new AlertDialog.Builder(context, R.style.TranslucentNoBar).show();
        dialog.setOnKeyListener(xxx);
        // 触摸dialog外边时dialog是否消失
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v);

        Window w = dialog.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.28); // 宽度设置为屏幕宽度的0.6--,全宽为1，最低是0
        //lp.height = (int) (d.heightPixels * 0.28); // 高度设置为屏幕的0.6
        lp.height = (int) (d.widthPixels * 0.28); // 高度设置为屏幕宽度的0.6
        // popupWindow.setWidth(display.widthPixels * 9 / 10);
        // popupWindow.setHeight(display.heightPixels * 9 / 10);
        // lp.alpha = 0.3f;
        w.setAttributes(lp);
        avi.show();
        // ============end
    }

    public static void hidn() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog.cancel();
            dialog = null;
            avi.hide();
        }
    }

    private static DialogInterface.OnKeyListener xxx = new DialogInterface.OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {// 按返回键对话框消失，透明度恢复 点击了手机返回键
                    try {
                        hidn();
                        ((Activity) mContext).finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return false;
        }
    };

}
